package hanelsoft.vn.dictionaylearn.control.service;

import hanelsoft.vn.dictionaylearn.R;
import hanelsoft.vn.dictionaylearn.model.db.VocabularySQLiteHelper;
import hanelsoft.vn.dictionaylearn.model.entity.InfoVocabularyContent;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class TooltipAlarmServiceLayoutParams extends Service{
	Intent mIntent;
	String tag ="[AlarmService]";
	PendingIntent pendingIntent;
	AlarmManager alarmManager;
	SharedPreferences share;
	VocabularySQLiteHelper mVocabularySQLiteHelper = new VocabularySQLiteHelper(this);
	ArrayList<InfoVocabularyContent> arrayVocabulary;
	InfoVocabularyContent infoVocabulary;
	@Override
	public void onCreate() {
		super.onCreate();
		mIntent = new Intent(this, TooltipRunServiceLayoutParams.class);
		Log.i(tag, "run create");
	}
	@Override
	@Deprecated
	public void onStart(Intent intent, int startId) {
		Log.i(tag, "run onStart");
		super.onStart(intent, startId);
	}
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.i(tag, "run onStartCommand");
		//
		share = getSharedPreferences("Time", 1);
		share.edit().putInt("isRunService", 1).commit();
		share.edit().putInt("positionVocabulary", 0).commit();
		String nameList = share.getString("namelist", null);
		arrayVocabulary = new ArrayList<InfoVocabularyContent>();
		arrayVocabulary = mVocabularySQLiteHelper.viewAllVocabularyContent(nameList);
		if(arrayVocabulary.size() < 1)
		{
			Toast.makeText(getBaseContext(), getString(R.string.vocabulary_is_null_please_add_vocabulary), Toast.LENGTH_SHORT).show();
			Intent i = new Intent(TooltipAlarmServiceLayoutParams.this, TooltipAlarmServiceLayoutParams.class);
			stopService(i);
		}
		else
		{
			startService(mIntent);
			int time = 10;
			Log.i(tag, "time = 5");
			time = share.getInt("time", 0);
			Log.i(tag, "time = "+time);
			pendingIntent = PendingIntent.getService(TooltipAlarmServiceLayoutParams.this, 0, mIntent, 0);
			alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeInMillis(System.currentTimeMillis());
			calendar.add(Calendar.SECOND, time);
			alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),time * 1000, pendingIntent);
			Log.i(tag, "run alarm");
		}
		return super.onStartCommand(intent, flags, startId);
	}
	@Override
	public void onDestroy() {
		super.onDestroy();
		if(arrayVocabulary.size() < 1)
		{
			Log.i(tag, "run onDestroy null");
		}
		else
		{
			share.edit().putInt("isRunService", 0).commit();
			alarmManager.cancel(pendingIntent);
			stopService(mIntent);
			Log.i(tag, "run onDestroy");
		}
	}
	@Override
	public IBinder onBind(Intent intent) {
		Log.i(tag, "run onBind");
		return null;
	}
	@Override
	public boolean onUnbind(Intent intent) {
		Log.i(tag, "run onUnbind");
		return super.onUnbind(intent);
	}
}
